package com.irun.pm.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.utils.Utils;

/**
 * 车辆信息查询
 * 
 * @author Evy
 * 
 */
public class CarQueryActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {
	private static final int REQUEST_ADD = 1;
	private List<DriverInfo> listFugitives = new ArrayList<DriverInfo>();
	private CardAdapter fugitiveAdapter;
	private ListView lvFugitives;
	private EditText etName, etCard;
	private DriverInfoControllers controller;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.car_query);
		findViewById(R.id.btnCardQuery).setOnClickListener(this);
		lvFugitives = (ListView) findViewById(R.id.lvFugitives);
		etName = (EditText) findViewById(R.id.etName);
		etCard = (EditText) findViewById(R.id.etCardNum);
		findViewById(R.id.btnAdd).setOnClickListener(this);
		lvFugitives.setOnItemClickListener(this);
		controller = new DriverInfoControllers(this);
		
		initData(null, null);
	}

	private void initData(String name, String cardNumber) {
		doProgress("提示", "请稍等,正在请求数据...", new Callable<String>() {
			@Override
			public String call() throws Exception {
				Thread.sleep(2000);
				return null;
			}
		}, new Callback<String>() {
			@Override
			public void onCallback(String pCallbackValue) {
				String name = etName.getText().toString();
				String cardNumber = etCard.getText().toString();
				listFugitives = controller.queryDriverInfoByCardNumberAndName(name, cardNumber, Utils.TYPE_CARINFO_QUERY);
				fugitiveAdapter = new CardAdapter(listFugitives);
				lvFugitives.setAdapter(fugitiveAdapter);
				//fugitiveAdapter.notifyDataSetChanged();
			}
		}, null);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnCardQuery:
			String name = etName.getText().toString().trim();
			String cardNumber = etCard.getText().toString().trim();
			initData(name, cardNumber);
			break;
		case R.id.btnAdd:
			Intent intent = new Intent(this, CarAddActivity.class);
			startActivityForResult(intent, REQUEST_ADD);
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DriverInfo driverInfo = (DriverInfo) parent.getItemAtPosition(position);
		Intent intent = new Intent(this, CarDetailsActivity.class);
		intent.putExtra("id", driverInfo.getQueryID());
		startActivity(intent);
	}

	class CardAdapter extends BaseAdapter {
		private List<DriverInfo> listFugitives;

		public CardAdapter(List<DriverInfo> listFugitives) {
			this.listFugitives = listFugitives;
		}

		@Override
		public int getCount() {
			return listFugitives.size();
		}

		@Override
		public Object getItem(int position) {
			return listFugitives.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(CarQueryActivity.this)
						.inflate(R.layout.car_item, null);
				holder = new ViewHolder();
				holder.tvCardNum = (TextView) convertView
						.findViewById(R.id.tvCardNum);
				holder.tvTime = (TextView) convertView
						.findViewById(R.id.tvTime);
				holder.tvNmae = (TextView) convertView
						.findViewById(R.id.tvName);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvCardNum.setText(listFugitives.get(position)
					.getCardNumber());
			holder.tvTime.setText(listFugitives.get(position).getDate());
			holder.tvNmae.setText(listFugitives.get(position).getName());
			return convertView;
		}

	}
	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");    
	class ViewHolder {
		TextView tvCardNum;
		TextView tvTime;
		TextView tvNmae;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_CANCELED) {
			return;
		}
		switch (requestCode) {
			case REQUEST_ADD:
				initData(null, null);
				break;
		}
	}
}
